package flexo.physic.force;

import flexo.math.Vector;
import flexo.physic.entity.Entity;
import flexo.scene.Node;

public class Spring extends Force
{
	Node hook;
	float length;
	Vector force;
	float k;

	public Spring(String name, Node hook, float length)
	{
		super(name);
		this.hook = hook;
		this.length = length;
		setElasticity(12);
		force = new Vector();
	}
	
	public void setElasticity(float value)
	{
		if (value >= 0)
		{
			this.k = -value;
		}
	}

	@Override
	public Vector calculateForce(Entity e, float deltaTime)
	{
		// Vector from the hook to the entity
		Vector.sub(e.getPosition(), hook.getAbsolutePosition(), force);
		float mag = force.getLength();
		
		force.normalize();
		// k is already negated in the constructor
		force.scale( (k * (mag - length)) );
		
		return force;
	}

}
